const Joi = require('joi');

class ProductValidate {
  // 产品列表参数校验
  async list(params) {
    const schema = Joi.object({
      name: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '名称不能包含特殊字符',
        }),
      type: Joi.string(),
      describe: Joi.string(),
      order: Joi.string()
        .default('create_time'),
      method: Joi.string()
        .default('desc'),
      pageIndex: Joi.string()
        .default('1'),
      pageSize: Joi.string()
        .default('10'),
    });
    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 新增产品校验
  async insert(params) {
    const schema = Joi.object({
      name: Joi.string()
        .required()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.empty': '名称不能为空',
          'string.pattern': '名称不能包含特殊字符',
          'string.required': '名称是必填项',
        }),
      describe: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '描述不能包含特殊字符',
        }),

      type: Joi.string()
        .required()
        .messages({
          'string.empty': '类型不能为空',
          'string.required': '类型是必填项',
        }),

      file_id: Joi.string().default('0'),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 更新产品参数校验
  async update(params) {

    const schema = Joi.object({
      id: Joi.string()
        .required()
        .messages({
          'string.empty': 'id不能为空',
          'string.required': 'id是必填项',
        }),
      name: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '描述不能包含特殊字符',
        })
        .default(null),

      describe: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '描述不能包含特殊字符',
        })
        .default(null),

      type: Joi.string().default(null),

      file_id: Joi.string().default(null),
    });


    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 删除产品参数校验
  async delete(params) {

    const schema = Joi.object({
      ids: Joi.array(),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }


  // 产品详情参数校验
  async info(params) {
    const schema = Joi.object({
      uuid: Joi.string()
        .required()
        .messages({
          'string.empty': 'uuid不能为空',
          'string.required': 'uuid为必填项',
        }),
    });


    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }
}


module.exports = new ProductValidate();
